package com.example.demo.service;

import com.example.demo.entity.MonitorobjInfo;
import com.example.demo.entity.MonitorobjPageInfo;
import com.example.demo.mapper.MonitorobjInfoMapper;
import com.example.demo.service.impl.BaseAccessService;
import com.example.demo.service.impl.TestDatabaseAccessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 自动预警类
 * 定时执行
 * Created by tanjun on 2021-12-30.
 */
@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class AutoWarning {
    @Autowired
    BaseAccessService baseAccessService;
    @Autowired
    MonitorobjInfoMapper monitorobjInfoMapper;


    //3.添加定时任务cron表达式
    @Scheduled(cron = "0 0/1 * * * ?")
    //或直接指定时间间隔，例如：10分钟
    //@Scheduled(fixedRate=600000)
    private void autoWarnTasks() {
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
        MonitorobjPageInfo param=new MonitorobjPageInfo();
        param.setRunStatus("monitoring");
        param.setPageNumber(1);
        param.setPageSize(100000);
        List<MonitorobjInfo> list=monitorobjInfoMapper.queryMonitorobjInfos(param);
        for(MonitorobjInfo mobj:list){
            TestAccessService testAccessService= baseAccessService.getTestAccessServiceByMonitorType(mobj.getMonitorobjType());
            testAccessService.testAccess(mobj.getMonitorobjId());
        }

    }
}

